<template>
  <div align="center" style="margin-top: 50px">
    <el-button type="success" @click="reflesh">刷新列表</el-button>
    <el-button type="warning" @click="loginOut">退出登录</el-button>
    <el-table
      :data="tableData"
      style="width: 80%"
    >
      <el-table-column
        prop="title"
        label="标题"
        :show-overflow-tooltip="true"
        :highlight-current-row="true"
      >
      </el-table-column>
      <el-table-column
        prop="author"
        label="作者"
      >
      </el-table-column>
      <el-table-column
        prop="do"
        label="操作">
        <template scope="scope">
          <el-button type="success" plain @click="detail(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChange"
      @current-change="nextPage"
      :page-sizes="[5, 10, 20, 50, 100, 500]"
      :page-size="size"
      layout="sizes, prev, pager, next"
      :total="1000"
    >
    </el-pagination>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :fullscreen="true"
      :before-close="handleClose"
    >
      <div v-html="content" style="height: 600px;width: 100%"></div>
    </el-dialog>
  </div>
</template>

<script>
import { getContentList } from '../../api/home'
import { loginOut } from '../../api/auth'
import { setItem } from '../../utils/storage'
import { getWidth } from '../../utils/client'

export default {
  name: 'List',
  data () {
    return {
      tableData: [],
      page: 1,
      size: 5,
      sum: 0,
      title: '查看',
      dialogVisible: false,
      content: ''
    }
  },
  mounted () {
    if (!localStorage.getItem(process.env.VUE_APP_TOKEN)) {
      this.$router.replace({
        path: '/'
      })
    }
    this.init(true)
  },
  methods: {
    detail (data) {
      this.content = data.content
      this.title = data.title
      this.dialogVisible = true
    },
    handleClose () {
      this.content = ''
      this.title = ''
      this.dialogVisible = false
    },
    init (reload) {
      if (reload) localStorage.removeItem('listData')
      this.tableData = JSON.parse(localStorage.getItem('listData'))
      if (!this.tableData) {
        getContentList({
          page: this.page,
          size: this.size
        }).then(res => {
          this.tableData = res.data
          this.sum = res.ext.sum
          setItem('listData', JSON.stringify(this.tableData), 10)
        })
      }
    },
    loginOut () {
      loginOut().then(res => {
        localStorage.removeItem(process.env.VUE_APP_TOKEN)
        this.$router.replace({
          path: '/'
        })
      }).catch(err => {
        localStorage.removeItem(process.env.VUE_APP_TOKEN)
        this.$message({
          message: '登出失败——' + err,
          type: 'error'
        })
      })
    },
    isMd () {
      return getWidth() > 992
    },
    reflesh () {
      this.init(true)
    },
    sizeChange (size) {
      this.size = size
      this.reflesh()
    },
    nextPage (page) {
      this.page = page
      this.reflesh()
    }
  }
}
</script>

<style scoped>

</style>
